package com.lwl.office.dcconverter.converter;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.lwl.office.dcconverter.cons.DCDocumentEnum;
import com.lwl.office.dcconverter.element.DCRoot;
import com.lwl.office.dcconverter.exception.ConverterInputError;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

/**
 * 转换器
 */
public abstract class Converter {
    private DCRoot root;
    private Object input;
    private static final Log log = LogFactory.get();
    /**
     * 解析input
     * @return
     */
    protected abstract DCRoot parse() ;

    /**
     * 根据type
     * @param type
     * @return
     */
    protected abstract Object out(DCDocumentEnum type);

    protected DCRoot getRoot() {
        return root;
    }

    protected Object getInput() {
        return input;
    }

    public static Converter build(Object input) throws ConverterInputError {
        Converter converter=null;
        if(input instanceof XWPFDocument){
            converter = new DCWordConverter();
        }else if(input instanceof String){
            converter = new DCHtmlConverter();
        }
        if(converter==null){
            throw new ConverterInputError(StrFormatter.format("暂不支持类型[{}]的文档转换",input.getClass()));
        }
        log.info("输入类型[{}],由转换器[{}]进行处理",input.getClass().getName(),converter.getClass().getName());
        converter.input = input;
        return converter;
    }


    public Object convert(DCDocumentEnum type){
        this.root = parse();
        return out(type);
    }
}
